/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ix.data.models;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ix.data.manager.POJOManager;

public class Relations {

    private transient Connection cnn = null;

    private final String basequery = "SELECT RPKID, FKITEMFROM, FKITEMTO, RDESCR, RFKTYPE FROM RELATIONS LEFT JOIN TYPES ON RFKTYPE = TYPES.TPKID";

    public Relations(Connection cnn) {
        this.cnn = cnn;
    }

    public synchronized ArrayList<Relation> loadIRelations(Relation filter) throws SQLException {
        String query = basequery;
        String where = POJOManager.createWhereClause(filter);

        if (where.length() > 0) {
            query += " WHERE " + where.toString();
        }

        ArrayList relations = new ArrayList(300);

        PreparedStatement prstmt = POJOManager.getPreparedStatement(cnn, query);
        prstmt = POJOManager.prepareStatement(prstmt, filter);
        ResultSet rset = prstmt.executeQuery();

        while (rset.next()) {
            Relation relation = new Relation();
            try {
                relation = (Relation) POJOManager.loadObject(rset, relation);
                relations.add(relation);
            } catch (Exception genexc) {
                Logger.getLogger(Relations.class.getName()).log(Level.SEVERE, null, genexc);
            }
        }

        POJOManager.close(rset, prstmt);

        return relations;
    }

    public synchronized Relation loadRelation(Relation filter) throws SQLException {
        String query = basequery;
        String where = POJOManager.createWhereClause(filter);

        if (where.length() > 0) {
            query += " WHERE " + where.toString();
        }

        PreparedStatement prstmt = POJOManager.getPreparedStatement(cnn, query);
        prstmt = POJOManager.prepareStatement(prstmt, filter);
        ResultSet rset = prstmt.executeQuery();

        Relation relation = null;
        if (rset.next()) {
            relation = new Relation();
            try {
                relation = (Relation) POJOManager.loadObject(rset, relation);
            } catch (Exception genexc) {
                Logger.getLogger(Relations.class.getName()).log(Level.SEVERE, null, genexc);
            }
        }

        POJOManager.close(rset, prstmt);

        return relation;
    }

    public synchronized int deleteRelations(Relation filter) throws SQLException {
        String query = "DELETE FROM RELATIONS";
        String where = POJOManager.createWhereClause(filter);

        if (where.length() == 0) {
            return 0;   // unable to delete all relations
        }

        query += " WHERE " + where.toString();

        PreparedStatement prstmt = POJOManager.getPreparedStatement(cnn, query);
        prstmt = POJOManager.prepareStatement(prstmt, filter);
        int rows = prstmt.executeUpdate();

        POJOManager.close(prstmt);

        return rows;
    }

    public static class Relation {
        public Long rpkid = null;
        public Long fkrelationfrom = null;
        public Long fkrelationto = null;
        public String rdescr = null;
    }
}
